<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;


/**
 * 素材库
 * Class Material
 * @package app\asset\controller
 */
class MaterialController extends AdminBaseController
{
    /**
     * 文件库(上传下载管理)
     */
    public function index()
    {   
        // $dir = config('upload_path');
        // $this->assign('name',  $dir);

        $fileType = "file";        

        $this->assign('filetype',  $fileType);
        $this->assign('size',  30);

        $param   = $this->request->param();
        $no_head = $this->request->param('no_head', 0, 'intval');
        $multi   = $this->request->param('multi',1);

        $this->assign('no_head', $no_head);
        $this->assign('multi', $multi);

        return $this->fetch();
    }

    /**
     * 图片库
     */
    public function library()
    {   
        // $dir      = config('upload_path');
        // $this->assign('name',  $dir);

        $fileType = "image";

        $this->assign('filetype',  $fileType);
        $this->assign('size',  30);

        $param   = $this->request->param();
        $no_head = $this->request->param('no_head', 0, 'intval');
        $multi   =  $this->request->param('multi',1);
        
        $this->assign('no_head', $no_head);
        $this->assign('multi', $multi);

        return $this->fetch();
    }

    //下载文件
    public function download() {
        $id       = $this->request->param('id', '','intval');

        $filePath = db('asset')->where('id', $id)->value('file_path');
        $filePath = iconv("utf-8","gb2312", $filePath);
        
        $uploadPath = config('upload_path');
        $filePath   = $uploadPath.$filePath;

        if (file_exists($filePath)) {
            header("Content-Type: application/force-download");
            header("Content-Disposition: attachment; filename=".basename($filePath));  
            readfile($filePath);
        }else {
            $this->error('文件不存在');
        }
    }
}

